//
//  PlayDetailProgressView.swift
//  TimMusic
//
//  Created by nkl on 2024/10/12.
//

import Foundation

class PlayDetailProgressView: UIView {
    lazy var progressBar: SJProgressSlider = {
        let bar = SJProgressSlider()
        bar.trackHeight = 4
        bar.trackImageView.backgroundColor = .lightGray
        bar.traceImageView.backgroundColor = .white
        return bar
    }()

    lazy var currentTime: UILabel = .simpleLabel(text: "00:00", font: .systemFont12, color: .white.withAlphaComponent(0.4))
    lazy var durationTime: UILabel = .simpleLabel(text: "00:00", font: .systemFont12, color: .white.withAlphaComponent(0.4))

    override init(frame: CGRect) {
        super.init(frame: frame)
        addChildren()
        makeConstraints()
    }

    public func updateTime(currentTime: TimeInterval, duration: TimeInterval) {
        let value = currentTime / duration
        progressBar.value = value
        self.currentTime.text = currentTime.mmsshh
        durationTime.text = duration.mmsshh
    }

    func addChildren() {
        addSubview(progressBar)
        addSubview(currentTime)
        addSubview(durationTime)
    }

    func makeConstraints() {
        progressBar.snp.makeConstraints { make in
            make.leading.trailing.bottom.equalToSuperview()
            make.height.equalTo(30)
        }

        currentTime.snp.makeConstraints { make in
            make.leading.equalToSuperview()
            make.bottom.equalTo(progressBar.snp.top)
            make.top.equalToSuperview().offset(8)
        }

        durationTime.snp.makeConstraints { make in
            make.trailing.equalToSuperview()
            make.bottom.equalTo(progressBar.snp.top)
            make.top.equalToSuperview().offset(8)
        }
    }

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
